package com.yoomiito.app.adapter.shopcar;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yoomiito.app.R;
import com.yoomiito.app.model.order.ShopCarGoods;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.widget.NumControlView;
import com.yoomiito.app.widget.RoundImageView;
import com.yoomiito.app.widget.SelectView;

import java.util.List;

public class ShopCarAdapter extends BaseQuickAdapter<ShopCarGoods, BaseViewHolder> {

    private OnSelectListener mItemClickListener;
    private OnNumChangeListener mOnNumChangeListener;
    private int mShopCarStatus = 1;

    public ShopCarAdapter(@Nullable List<ShopCarGoods> data) {
        super(R.layout.item_shop_car_goods, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ShopCarGoods item) {
        SelectView selectView = helper.getView(R.id.selectView);
        selectView.setSelect(item.isSelect());
        if (mShopCarStatus == 1) {
            if (item.getIs_deleted() == 1) {
                selectView.setUnClickSelect(R.drawable.icon_unselect_gray);
            } else {
                selectView.setEnabled(true);
            }
            helper.setGone(R.id.no_goods_bg_2, false);
        } else {//编辑状态
            selectView.setEnabled(true);
            helper.setGone(R.id.no_goods_bg_2, item.getIs_deleted() == 1);
        }

        RoundImageView imageView = helper.getView(R.id.goods_pic);
        ImageLoader.getInstance().into(imageView.getContext(), item.banner, imageView, R.drawable.goods_default);
        NumControlView numControlView = helper.getView(R.id.num_control_view);
        numControlView.setNum(item.getNum());
        numControlView.setNoClick(item.getIs_deleted() == 0);
        numControlView
                .setListener(num -> {
                    if (mOnNumChangeListener != null) {
                        mOnNumChangeListener.numChange(helper.getLayoutPosition(), num);
                    }
                })
                .addOnInputNumChangeListener(num -> {
                    if (mOnNumChangeListener != null) {
                        mOnNumChangeListener.numChange(helper.getLayoutPosition(), num);
                    }
                });

        helper.setText(R.id.goods_title, item.getProduct_name());
        helper.setText(R.id.goods_price, SpannableUtils.getSmallMoneyFlag("¥" + item.getSale_price()));
        helper.setVisible(R.id.no_goods_bg, mShopCarStatus == 1 ? item.getIs_deleted() == 1 : false);
        helper.setVisible(R.id.no_goods_content, item.getIs_deleted() == 1);
        if (mItemClickListener != null)
            selectView.setClickListener(v -> {
                if (mItemClickListener != null) {
                    item.setSelect(selectView.isSelected());
                    mItemClickListener.select(helper.getLayoutPosition(), selectView.isSelected());
                }
            });
    }

    public void setItemClickListener(OnSelectListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public void setOnNumChangeListener(OnNumChangeListener onNumChangeListener) {
        mOnNumChangeListener = onNumChangeListener;
    }

    public void setShopCarStatus(int shopCarStatus) {
        mShopCarStatus = shopCarStatus;
        notifyDataSetChanged();
    }

    public interface OnSelectListener {
        void select(int position, boolean isSelected);
    }

    public interface OnNumChangeListener {
        void numChange(int position, int num);
    }
}
